<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="86px">
      <el-form-item label="群ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入群ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入群名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="群认证" prop="certiStatus">
        <el-select v-model="queryParams.certiStatus" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in certiStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>-->
<!--      <el-form-item label="群成员保护" prop="memberProtection">
        <el-select v-model="queryParams.memberProtection" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in memberProtectionOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="全员禁言" prop="isMute">
        <el-select v-model="queryParams.isMute" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in isMuteOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="群状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['model:groups:add']"
        >新增</el-button>
      </el-col>-->

      <el-col :span="1.5">
        <el-button
          type="danger"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['model:groups:remove']"
        >解散群</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['model:groups:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="groupsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="群id" align="center" prop="id" />
      <el-table-column label="群名称" align="center" prop="name" show-overflow-tooltip/>
      <el-table-column label="群头像" align="center" prop="portraitUri" >
        <template slot-scope="scope">
          <el-avatar shape="square" :src=" scope.row.portraitUri" :style="randomRgb()" :size="50">
            {{scope.row.name}}
          </el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="群主" align="center" prop="createName" show-overflow-tooltip/>
      <el-table-column label="群成员数量" align="center" prop="memberCount" />
      <!--<el-table-column label="公告" align="center" prop="bulletin" />-->
      <el-table-column label="群认证" align="center" prop="certiStatus" :formatter="certiStatusFormat" />
      <el-table-column label="群成员保护" align="center" prop="memberProtection" :formatter="memberProtectionFormat" />
      <el-table-column label="全员禁言" align="center" prop="isMute" :formatter="isMuteFormat" />
      <!--<el-table-column label="清理选项" align="center" prop="clearStatus" />-->
      <!--<el-table-column label="清理时间" align="center" prop="clearTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.clearTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="getMembersList(scope.row)"
            v-hasPermi="['model:groups:edit']"
          >群成员</el-button>
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:groups:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改群信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="创建人" prop="createId">
          <el-input v-model="form.createId" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="群名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入群名称" />
        </el-form-item>
        <el-form-item label="群头像" prop="portraitUri">
          <el-input v-model="form.portraitUri" placeholder="请输入群头像" />
        </el-form-item>
        <el-form-item label="群成员数量" prop="memberCount">
          <el-input v-model="form.memberCount" placeholder="请输入群成员数量" />
        </el-form-item>
        <el-form-item label="公告" prop="bulletin">
          <el-input v-model="form.bulletin" placeholder="请输入公告" />
        </el-form-item>
        <el-form-item label="开启群认证: 0-开启;1-关闭">
          <el-radio-group v-model="form.certiStatus">
            <el-radio
              v-for="dict in certiStatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="群成员保护:0-关闭;1-开启">
          <el-radio-group v-model="form.memberProtection">
            <el-radio
              v-for="dict in memberProtectionOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="全员禁言状态:0-否;1-是">
          <el-radio-group v-model="form.isMute">
            <el-radio
              v-for="dict in isMuteOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="清理选项:0-关闭;3-3天前;7-7天前;36-36小时前">
          <el-radio-group v-model="form.clearStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="清理时间" prop="clearTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.clearTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择清理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态:0-正常;5-删除">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 所在群对话框 -->
    <el-dialog title="群成员" :visible.sync="openMembers" width="800px" append-to-body>
      <el-table v-loading="loadingMembers" :data="membersList">
        <el-table-column label="拓聊号" align="center" >
          <template slot-scope="scope">
            {{scope.row.user.cloudId}}
          </template>
        </el-table-column>
        <el-table-column label="昵称" align="center"  show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.user.nickName}}
          </template>
        </el-table-column>
        <el-table-column label="群昵称" align="center" prop="groupNickName" show-overflow-tooltip/>
        <el-table-column label="角色" align="center" prop="role"  :formatter="roleFormat"/>
        <el-table-column label="入群方式" align="center" :formatter="joinTypeFormat" show-overflow-tooltip/>
        <el-table-column label="入群时间" align="center" width="170" prop="joinTime"/>
      </el-table>
      <pagination
        v-show="totalMembers>0"
        :total="totalMembers"
        :page-sizes="[8,16]"
        :page.sync="queryMembersParams.pageNum"
        :limit.sync="queryMembersParams.pageSize"
        @pagination="getMembersList"
      />
    </el-dialog>


  </div>
</template>

<style>
  .el-avatar{
    margin-top: 6px;
  }
</style>

<script>
import { listGroups,members, getGroups, delGroups, addGroups, updateGroups, exportGroups } from "@/api/model/groups";

export default {
  name: "Groups",
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingMembers: true,
      openMembers: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      totalMembers: 0,
      // 群信息表格数据
      groupsList: [],
      groupId:null,
      // 群成员
      membersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 开启群认证: 0-开启;1-关闭字典
      certiStatusOptions: [],
      // 群成员保护:0-关闭;1-开启字典
      memberProtectionOptions: [],
      roleOptions: ['群主','管理员','普通成员'],
      joinTypeOptions: ['创建者','邀请入群','扫码入群'],
      // 全员禁言状态:0-否;1-是字典
      isMuteOptions: [],
      // 状态:0-正常;5-删除字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createId: undefined,
        name: undefined,
        portraitUri: undefined,
        memberCount: undefined,
        bulletin: undefined,
        certiStatus: undefined,
        memberProtection: undefined,
        isMute: undefined,
        clearStatus: undefined,
        clearTime: undefined,
        status: '0',
      },
      queryMembersParams: {
        pageNum: 1,
        pageSize: 8,
      },
      baseUrl:process.env.VUE_APP_BASE_API,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("group_certi_status").then(response => {
      this.certiStatusOptions = response.data;
    });
    this.getDicts("group_member_protection").then(response => {
      this.memberProtectionOptions = response.data;
    });
    this.getDicts("group_is_mute").then(response => {
      this.isMuteOptions = response.data;
    });
    this.getDicts("group_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询群信息列表 */
    getList() {
      this.loading = true;
      listGroups(this.queryParams).then(response => {
        this.groupsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMembersList(row){
      this.loadingMembers = true;
      this.openMembers = true;
      if(row.id!=null){
        this.groupId = row.id;
      }
      members(this.groupId,this.queryMembersParams).then(res=>{
        this.membersList = res.rows;
        this.totalMembers = res.total;
        this.loadingMembers = false;
      })
    },
    //随机头像背景颜色
    randomRgb(item) {
      let R = Math.floor(Math.random() * 130+110);
      let G = Math.floor(Math.random() * 130+110);
      let B = Math.floor(Math.random() * 130+110);
      return {
        'background-color': 'rgb(' + R + ',' + G + ',' + B + ')'
      };
    },
    // 开启群认证: 0-开启;1-关闭字典翻译
    certiStatusFormat(row, column) {
      return this.selectDictLabel(this.certiStatusOptions, row.certiStatus);
    },
    // 群成员保护:0-关闭;1-开启字典翻译
    memberProtectionFormat(row, column) {
      return this.selectDictLabel(this.memberProtectionOptions, row.memberProtection);
    },
    // 全员禁言状态:0-否;1-是字典翻译
    isMuteFormat(row, column) {
      return this.selectDictLabel(this.isMuteOptions, row.isMute);
    },
    // 状态:0-正常;5-删除字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    roleFormat(row, column) {
      return this.roleOptions[row.role];
    },
    // 入群方式
    joinTypeFormat(row, column) {
      if(row.joinType === 1){
        return row.inviteeName+"邀请入群";
      }
      return this.joinTypeOptions[row.joinType];
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createId: undefined,
        name: undefined,
        portraitUri: undefined,
        memberCount: undefined,
        bulletin: undefined,
        certiStatus: "0",
        memberProtection: "0",
        isMute: "0",
        clearStatus: "0",
        clearTime: undefined,
        status: "0",
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加群信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGroups(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改群信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateGroups(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGroups(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除群信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGroups(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有群信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGroups(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
